const Controller = require('egg').Controller;

class CirclePostController extends Controller {
    // 创建圈子帖子
    async create() {
        const { ctx, service } = this;

        try {
            // 参数验证
            const rules = {
                circle_id: { type: 'number', required: true },
                content: { type: 'string', required: true },
                images: { type: 'array', required: false }
            };
            ctx.validate(rules);

            const postData = ctx.request.body;

            // 从token中获取用户ID
            const token = ctx.request.header.token;
            const decode = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);

            const result = await service.circlePost.create({
                ...postData,
                user_id: decode.user_id
            });

            ctx.body = {
                code: 0,
                data: result,
                msg: '发布成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '发布失败'
            };
        }
    }

    // 获取圈子帖子列表
    async list() {
        const { ctx, service } = this;

        try {
            const { circle_id } = ctx.params;
            const query = ctx.query;

            // 验证圈子ID参数
            const rules = {
                circle_id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.params);

            const result = await service.circlePost.getCirclePosts(circle_id, query);

            ctx.body = {
                code: 0,
                data: result,
                msg: '获取成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取帖子列表失败'
            };
        }
    }

    // 获取帖子详情
    async detail() {
        const { ctx, service } = this;

        try {
            const { id } = ctx.params;

            // 验证ID参数
            const rules = {
                id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.params);

            const post = await service.circlePost.getPostWithUser(id);

            ctx.body = {
                code: 0,
                data: post,
                msg: '获取成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取帖子详情失败'
            };
        }
    }

    // 更新帖子信息
    async update() {
        const { ctx, service } = this;

        try {
            const { id } = ctx.params;
            const updateData = ctx.request.body;

            // 验证路径参数
            const paramRules = {
                id: { type: 'id', required: true }
            };
            ctx.validate(paramRules, ctx.params);

            // 验证body参数
            const bodyRules = {
                content: { type: 'string', required: false },
                images: { type: 'array', required: false }
            };
            ctx.validate(bodyRules);

            // 从token中获取用户ID
            const token = ctx.request.header.token;
            const decode = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);

            const result = await service.circlePost.updatePost(id, updateData, decode.user_id);

            ctx.body = {
                code: 0,
                data: result,
                msg: '更新成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '更新帖子失败'
            };
        }
    }

    // 删除帖子
    async delete() {
        const { ctx, service } = this;

        try {
            const { id } = ctx.params;

            // 验证ID参数
            const rules = {
                id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.params);

            // 从token中获取用户ID
            const token = ctx.request.header.token;
            const decode = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);

            await service.circlePost.deletePost(id, decode.user_id);

            ctx.body = {
                code: 0,
                data: null,
                msg: '删除成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '删除帖子失败'
            };
        }
    }

    // 点赞帖子
    async like() {
        const { ctx, service } = this;

        try {
            const { id } = ctx.params;

            // 验证ID参数
            const rules = {
                id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.params);

            const result = await service.circlePost.likePost(id);

            ctx.body = {
                code: 0,
                data: result,
                msg: '点赞成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '点赞失败'
            };
        }
    }

    // 取消点赞帖子
    async unlike() {
        const { ctx, service } = this;

        try {
            const { id } = ctx.params;

            // 验证ID参数
            const rules = {
                id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.params);

            const result = await service.circlePost.unlikePost(id);

            ctx.body = {
                code: 0,
                data: result,
                msg: '取消点赞成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '取消点赞失败'
            };
        }
    }

    // 获取用户在所有圈子中的帖子
    async userPosts() {
        const { ctx, service } = this;

        try {
            // 从token中获取用户ID
            const token = ctx.request.header.token;
            const decode = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);

            const query = ctx.query;
            const result = await service.circlePost.getUserCirclePosts(decode.user_id, query);

            ctx.body = {
                code: 0,
                data: result,
                msg: '获取成功'
            };
        } catch (error) {
            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取用户帖子失败'
            };
        }
    }

    // 获取热门圈子帖子
    async hot() {
        const { ctx, service } = this;

        try {
            const { limit = 10 } = ctx.query;
            const posts = await service.circlePost.getHotCirclePosts(limit);

            ctx.body = {
                code: 0,
                data: posts,
                msg: '获取成功'
            };
        } catch (error) {
            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取热门帖子失败'
            };
        }
    }

    // 搜索圈子帖子
    async search() {
        const { ctx, service } = this;

        try {
            const { keyword, circle_id } = ctx.query;

            if (!keyword || keyword.trim() === '') {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: '搜索关键词不能为空'
                };
                return;
            }

            const result = await service.circlePost.searchCirclePosts(keyword.trim(), circle_id, ctx.query);

            ctx.body = {
                code: 0,
                data: result,
                msg: '搜索成功'
            };
        } catch (error) {
            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '搜索帖子失败'
            };
        }
    }
}

module.exports = CirclePostController;